#include "toast.h"

Toast::Toast(const QString& text)
{
    //设置核心属性
    this->setFixedSize(400,200);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); border-radius: 10px; }");

    //设置弹窗坐标
    QScreen* screen = QApplication::primaryScreen();
    int PWidth = screen->size().width();
    int PHeight = screen->size().height();

    int x = PWidth - 400 - 50;
    int y = PHeight - 200 - 50;

    this->move(x,y);

    //两秒后自动关闭
    QTimer* timer = new QTimer(this);
    timer->start(2000);

    connect(timer,&QTimer::timeout,this,[=](){
        timer->stop();
        this->close();
    });

    //创建垂直布局管理器
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    this->setLayout(layout);

    //创建文本
    QLabel* label = new QLabel();
    label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    label->setAlignment(Qt::AlignCenter);
    label->setText(text);
    QFont font;
    font.setPointSize(18);
    label->setFont(font);

    layout->addWidget(label);

}

void Toast::showMessage(const QString &text)
{
    Toast* toast = new Toast(text);
    toast->show();
}
